package org.omich.loaves;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ComputerPlayer extends Thread {
	private IBrain mBrain;

	ComputerPlayer(IBrain brain) {
			mBrain = brain;
	}
	public void run() {
		for (;;)
		{
			HashMap<Integer, Integer> freeLoaves = mBrain.getStatus().getFreeLoaves();
			int randomNumber = randomInt(freeLoaves.size());
			int i = 0;
			for (Map.Entry<Integer, Integer> e : freeLoaves .entrySet()) {
				if (i == randomNumber);
					mBrain.tryToEatLoaf("computer", e.getKey());
					i++;
			}
			try {
				sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
		
	private static Random random = new Random();
	private static int randomInt(int n) {
		if (n == 0)
			return -1;
		return Math.abs(random.nextInt()) % n;
	}

}
